document.write("<script language=javascript src='../js/validate.js'></script>");
//租户根权限维护（软件卖方给企业买家维护的地方）
			var app=angular.module('rootTenantAuthority',['ngRoute'])
            app.config(['$routeProvider', function($routeProvider){
		               	$routeProvider.when('/rootTenant', {
										templateUrl: 'rootTenantAuthority.html',
										controller: 'rootTenantAuthorityCtrl'
							});
            }]);
            
				app.controller("rootTenantAuthorityCtrl", function($scope,$location,$http,$window) {
					$(".modal-dialog").draggable();//为模态对话框添加拖拽
       				 $(".modal").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
					console.log(resultPath)
            		$scope.getJson = function(resultPath,data,method,successfn){
            			$http({
            	                method: method, 
            	                dataType:"json",
            	                url: resultPath,
            	                data:data,
            	                headers: {
            	                	'Content-Type': 'application/json',
            	                	'token_id':'11'
	         	                }
            	            }).
            	            success(function(data) {
														successfn(data);
            	            }).
            	            error(function(data, status) {
            	            	
            	            });
            	}
            		
            		//与数据库交互成功后右上角提示
            	$scope.showSuccess = function(str){
            		$('#successSave').fadeIn('slow')
            		if(str==''){
            			$('#alertStr').html('操作成功')
            		}else{
            			$('#alertStr').html(str)
            		}
            		setTimeout("$('#successSave').fadeOut('slow')",3000);
            	}
            	//与数据库交互失败后右上角提示 str 为要写入的提示文字
            	$scope.showError = function(str){
            		$('#errorSave').fadeIn('slow')
            		if(str==''){
            			$('#errorStr').html('操作失败')
            		}else{
            			$('#errorStr').html(str)
            		}
            		setTimeout("$('#errorSave').fadeOut('slow')",3000);
            	}
            	
            		$scope.tenants = [];
            		$scope.tenantId = '';
            		$scope.tenantFullName = '';
            		$scope.enterpriseDatas=[];
            		//获取所有用户
            		$scope.getTenants = function(){
            				$scope.getJson(resultPath+'/wisdomeatery/v1/tenants','','GET',function(data){
												console.log(data);
												$scope.tenants = data.data;
												console.log($scope.tenants);
												$scope.tenantId = '';
												$scope.tenantFullName = '';
										})
            		}
            		$scope.getTenants();
            		//获取卖方所有权限
            		$scope.getAllRootAuthorities = function(){
            			$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/getAllRootAuthorities','','GET',function(data){
												console.log(data);
												$scope.datas = data.data;
										})
            		}
            		$scope.getAllRootAuthorities();
            		//关闭权限维护模板
            		$scope.authority_true = function(){
            			$("#authorityWeihu").modal('toggle');
            			$scope.getAllRootAuthorities();
            		}
            		//点击某个用户，获取权限
					$scope.getAuthority = function(event,tenantId,tenantFullName){
							event.stopPropagation();
							$scope.tenantId = tenantId ;
							$scope.tenantFullName = tenantFullName ;
							
							var target = event.target ;
							$(target).siblings().each(function(){
			   					$(this).css('background-color','#ffffff');
			   			});
				   		$(target).css('background-color','#e4eaec');
						//查询此用户拥有权限
						$scope.getJson(resultPath+"/wisdomeatery/v1/tenants/getAuthorityByTenant/"+$scope.tenantId,'',"get",function(data){
									console.log(data)
									$scope.datas = data.data;
							})
						//查询此租户下的所有企业
						$scope.getJson(resultPath+"/wisdomeatery/v1/tenants/"+$scope.tenantId+"/enterprise",'',"get",function(data){
									console.log(data)
									$scope.enterpriseDatas =data.data;
									$scope.enterpriseId = '';//置空企业ID
									$scope.regionDatas = [];
									$scope.regionId = '';//置空区域ID
									$scope.canteenDatas = [];
							})
					}
            		//展开缩放树结构模板
					$scope.modalDiv = function(id){
							console.log(id);
							console.log( $('.repeatDiv_'+id).css('display') );
							$('.repeatDiv_'+id).slideToggle("slow");
					}
						//新增用户名称时，
	            $scope.judgeShow = function(){
	            	if( $scope.tenantAddName == '' || $scope.tenantAddName == undefined){
	            		 $scope.showIt = true;
	            	}else{
	            		$scope.showIt = false;
	            	}
	            	if( $scope.tenantAddUserName == '' || $scope.tenantAddUserName == undefined){
	            		 $scope.showItUserName = true;
	            	}else{
	            		$scope.showItUserName = false;
	            	}
	            	if( $scope.tenantAddPassWord == '' || $scope.tenantAddPassWord == undefined){
	            		 $scope.showItPassWord = true;
	            	}else{
	            		$scope.showItPassWord = false;
	            	}
	            }
	              //修改用户名称时，
	            $scope.judgeShow1 = function(){
	            	if( $scope.tenantFullName == '' || $scope.tenantFullName == undefined){
	            		 $scope.showUpdate = true;
	            	}else{
	            		$scope.showUpdate = false;
	            	}
	            }
						
						//打开新增用户模板
							$scope.openTenantModal = function(){
									$scope.tenantAddName = '';
									$("#addTenant").modal('toggle');
							}
							//确定新增用户
							$scope.addTenant_true = function(){
									if( isNull($scope.tenantAddName) == true){
										$scope.showIt = true;
										return;
									}
									if( isNull($scope.tenantAddUserName) == true){
										$scope.showIt = true;
										return;
									}
									if( isNull($scope.tenantAddPassWord) == true){
										$scope.showIt = true;
										return;
									}
									var postData = {
											'fullName':$scope.tenantAddName,
											'userName':$scope.tenantAddUserName,
											'passWord':$scope.tenantAddPassWord,
									}
									console.log(666);
									$scope.getJson(resultPath+'/wisdomeatery/v1/tenants',postData,'POST',function(data){
												console.log(data);
												if(data.code == 0){
														$scope.getTenants();
														$scope.showSuccess('操作成功');
														$("#addTenant").modal('toggle');
												}else{
														$scope.showError('操作失败');
												}
												
										})
							}
							//打开修改用户模板
							$scope.openUpdateTenant = function(){
								if( $scope.tenantId == ''){
									$scope.showError('请选择用户');
									return;
								}
									$("#updateTenant").modal('toggle');
							}
							//修改用户信息
							$scope.updateTenant_true = function(){
									if( isNull($scope.tenantFullName) == true){
											$scope.showUpdate = true;
											return;
										}
										var postData = {
												'id':$scope.tenantId,
												'fullName':$scope.tenantFullName
										}
										$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/'+$scope.tenantId,postData,'PUT',function(data){
													console.log(data);
													if(data.code == 0){
														$scope.showSuccess('操作成功');
														$("#updateTenant").modal('toggle');
															//获取用户
							            		$scope.getTenants();
													}else{
															$scope.showError('操作失败');
													}
											})
							}
							//打开删除用户确认模板
							$scope.delTenant = function(){
								if( $scope.tenantId == ''){
									$scope.showError('请选择用户');
									return;
								}
									$("#delTenant").modal('toggle');
							}
							//删除用户
							$scope.del_Tenant = function(){
									$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/'+$scope.tenantId,'','DELETE',function(data){
													console.log(data);
													if(data.code == 0){
														$scope.tenantId = '';
														$scope.showSuccess('操作成功');
														$("#delTenant").modal('toggle');
															//获取用户
							            		$scope.getTenants();
													}else{
															$scope.showError('操作失败');
													}
											})
							}
							//打开权限维护模板
							$scope.openAuthorityMaintain = function(){
								$("#authorityWeihu").modal('toggle');
							}
							$scope.authoritys = [];
							//查询1级权限
							$scope.searchNextLevelAuthority = function(pid){
									$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/anthority/'+pid,'','GET',function(data){
													console.log(data);
													if(data.code == 0){
														$scope.authoritys = data.data;
														$scope.oneId = '';
													}else{
													}
											})
							}
							//获取1级权限
							$scope.searchNextLevelAuthority('0');
							$scope.level = 1;
							$scope.oneId = '';
							$scope.twoId = '';
							$scope.authId = '';
							$scope.authName= '';
							$scope.parentId = '';
							$scope.updatePid = '';
							//点击主目录权限
							$scope.getSecondAuthority = function(id){
									$scope.oneId = id;
									$('.oneLevel').each(function(){
											$(this).css('background-color','#ffffff');
									});
									$('#oneLevel_'+id).css('background-color','#e4eaec');
								
									$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/anthority/'+id,'','GET',function(data){
													console.log(data);
													if(data.code == 0){
														$scope.secondAuthoritys = data.data;
														$scope.twoId = '';
														$scope.thirdAuthoritys = [];
													}else{
													}
											})
							}
							//点击二级目录权限
							$scope.getThirdAuthority = function(id){
									$scope.twoId = id;
									$('.twoLevel').each(function(){
											$(this).css('background-color','#F8FAFF');
									});
									$('#twoLevel_'+id).css('background-color','#e4eaec');
									
									$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/anthority/'+id,'','GET',function(data){
													console.log(data);
													if(data.code == 0){
														$scope.thirdAuthoritys = data.data;
													}else{
													}
											})
							}
							
							  //打开新建一级权限
						  $scope.addOneLevelAuthority = function(){
						  		$scope.level = 1;
						  		$scope.resourceName = '';
						  		$scope.resourcePath = '';
						  		$("#addResource").modal('toggle');
						  }
						  //打开新建二级权限
						  $scope.addTwoLevelAuthority = function(){
						  		$scope.level = 2;
						  		if( $scope.oneId == ''){
										$scope.showError('请选择主目录');
										return;
									}
						  		$scope.resourceName = '';
						  		$scope.resourcePath = '';
						  		$scope.description = '';//备注
						  		$("#addResource").modal('toggle');
						  }
						   //打开新建三级权限
						  $scope.addThreeLevelAuthority = function(){
						  		$scope.level = 3;
						  		if( $scope.twoId == ''){
										$scope.showError('请选择二级目录');
										return;
									}
						  		$scope.resourceName = '';
						  		$scope.resourcePath = '';
						  		$("#addResource").modal('toggle');
						  }
						  
						  //新建权限模板确定键
						  $scope.addResource_true = function(){
						  	
						  	if( $scope.level == '1'){
						  		$scope.parentId = '0';
						  	}else if($scope.level == '2'){
						  		$scope.parentId = $scope.oneId;
						  	}else{
						  		$scope.parentId = $scope.twoId;
						  	}
						  	console.log($scope.resourceName);
						  	console.log($scope.resourcePath);
						  	console.log($scope.level);
						  	console.log($scope.parentId);
						  	
						  	var postData ={
						  		'name':$scope.resourceName,
						  		'description':$scope.description,
						  		'path':$scope.resourcePath,
						  		'level':parseInt($scope.level)
						  	}
						  	$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/anthority/'+$scope.parentId,postData,'POST',function(data){
									console.log(data);
									if(data.code == 0){
										$scope.showSuccess('操作成功');
										$("#addResource").modal('toggle');
											$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/anthority/'+$scope.parentId,'','GET',function(data){
													console.log(data);
													if(data.code == 0){
														if( $scope.level == '1'){
													  		$scope.authoritys = data.data;
															$scope.oneId = '';
															$scope.secondAuthoritys = [];
													  	}else if($scope.level == '2'){
													  		$scope.secondAuthoritys = data.data;
															$scope.twoId = '';
															$scope.thirdAuthoritys = [];
													  	}else{
													  		$scope.thirdAuthoritys = data.data;
						  								}
													}else{
													}
											})
									}else{
										$scope.showError('操作失败');
									}
							})
						  	
						  }
						$scope.updateLevel = '';
						$scope.updatePid = '';
						//打开权限编辑模板
						$scope.AuthorityEdit = function(id,name,path,level,parentId,description){
							console.log(description);
							$scope.authId = id ;
							$scope.updateresourceName = name ;
							$scope.updateDescription = description ;
							$scope.updateresourcePath = path ;
							$scope.updateLevel = level;
							$scope.updatePid = parentId;
							$("#updateResource").modal('toggle');
						}
						//修改权限模板确定键  
						 $scope.updateResource_true = function(){
						 	var postData = {
						 		'name':$scope.updateresourceName,
						 		'description':$scope.updateDescription,
						 		'path':$scope.updateresourcePath
						 	}
						 	$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/anthority/'+$scope.authId,postData,'PUT',function(data){
										console.log(data);
										if(data.code == 0){
											$("#updateResource").modal('toggle');
											if( $scope.updateLevel == '1'){
												$scope.searchNextLevelAuthority('0');
											}else if( $scope.updateLevel == '2'){
												$scope.getSecondAuthority($scope.updatePid);
											}else{
												$scope.getThirdAuthority($scope.updatePid);
											}
										}else{
										}
								})
						 }
						 $scope.delLevel = '';
						 $scope.delPid = '';
						//打开删除权限提示模板
						$scope.AuthorityDel = function(id,name,path,level,parentId){
							$scope.authId = id ;
							$scope.authName = name ;
							$scope.delLevel = level;
							$scope.delPid = parentId ;
							$("#delAuthority").modal('toggle');
						}
						//删除模板确定键
						$scope.del_authority = function(id){
							$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/anthority/'+id,'','DELETE',function(data){
										console.log(data);
										if(data.code == 0){
											$("#delAuthority").modal('toggle');
											if( $scope.delLevel == '1'){
												$scope.searchNextLevelAuthority('0');
											}else if( $scope.delLevel == '2'){
												$scope.getSecondAuthority($scope.delPid);
											}else{
												$scope.getThirdAuthority($scope.delPid);
											}
										}else{
											$scope.showError(data.msg);
										}
								})
						}
						
			//保存用户权限
			$scope.saveAuthority = function(){
				console.log($scope.tenantId);
					if( $scope.tenantId == ''){
						$scope.showError('请选择用户');
						return;
					}
					console.log($scope.datas);
					//保存用户权限
					$scope.getJson(resultPath+"/wisdomeatery/v1/tenants/saveAuthorityByTenant/"+$scope.tenantId,$scope.datas,"PUT",function(data){
								console.log(data);
								if( data.code == 0){
								$scope.showSuccess('操作成功');
								//重新查询权限
								$scope.getJson(resultPath+"/wisdomeatery/v1/tenants/getAuthorityByTenant/"+$scope.tenantId,'',"get",function(data){
											console.log(data)
											$scope.datas = data.data;
									})
								}
							 
						})
			}
			//判断上下级选择状态
			$scope.judgeState = function(event,id,pid,level){
						event.stopPropagation();
						console.log(id);
						console.log(pid);
						console.log(level);
						var target = event.target;
						var arr = $scope.datas.children;
						console.log(arr);
						var state = $(target).is(':checked');
						if( state ){//将上级或者上上级的状态也设置为true
//												$('#checkbox_'+pid).prop('checked',true);
//												if( pid != 0){
//													$('#checkbox_'+$('#checkbox_'+pid).attr('class').substring(9,13)).prop('checked',true);
//												}
									if( level == 1){
											for( var i = 0 ;i < arr.length ; i++){
											if( arr[i].id ==  id){
												var twoLevelArray = arr[i].children;
												for( var j = 0; j< twoLevelArray.length ; j++ ){
													twoLevelArray[j].checked = true ;
												}
											}
										}
										return;
									}
									if( level == 2){
											for( var i = 0 ;i < arr.length ; i++){
													if( arr[i].id ==  pid){
															arr[i].checked = true ;
													}
											}
									}
									if( level == 3 ){
											var ppid = '';
											for( var i = 0 ;i < arr.length ; i++){
												var newChildrenArr = arr[i].children;
												for( var j = 0 ;j < newChildrenArr.length ; j++ ){
														if( newChildrenArr[j].id ==  pid){
																newChildrenArr[j].checked = true ;
																ppid = newChildrenArr[j].parentId ;
														}
												}
											}
											for( var i = 0 ;i < arr.length ; i++){
													if( arr[i].id ==  ppid){
															arr[i].checked = true ;
													}
											}
									}



						}
						//判断是否可以取消选择
						if( level == 1 ){
								for( var i = 0 ;i < arr.length ; i++){
									var newChildrenArr = arr[i].children;
									for( var j = 0 ;j < newChildrenArr.length ; j++ ){
											if( newChildrenArr[j].parentId ==  id && newChildrenArr[j].checked ){
													$scope.showError('子分类中有被选项时，无法撤销选择');
													arr[i].checked = true ;
													return;
											}
									}
								}
						}
						
						if( level == 2 ){
								var level3Array = [];
								for( var i = 0 ;i < arr.length ; i++){
									var newChildrenArr = arr[i].children;
									for( var j = 0 ;j < newChildrenArr.length ; j++ ){
											if( newChildrenArr[j].id ==  id ){
													level3Array = newChildrenArr[j].children ;
													for( var k = 0;k< level3Array.length ; k++){
															if( level3Array[k].checked ){
																$scope.showError('子分类中有被选项时，无法撤销选择');
																 newChildrenArr[j].checked = true ;
																 return;
															}
													}
											}
									}
								}
						}
			}
				
			//展开隐藏租户维护面板
			$scope.TenantWeiHu = function(){
				$("#TenantWeiHu").slideToggle();
			}
			//展开新增企业模板
			$scope.addEnterprise = function(){
				if( $scope.tenantId == ''){
					$scope.showError('请选择租户');
					return;
				}
				$("#addEnterprise").modal('toggle');
			}
			//新增企业
			$scope.addEnterprise_true = function(){
				if( $scope.enterpriseName.length > 45){
					$scope.showError('企业名称长度不能大于45位');
					return;
				}
				if( $scope.enterpriseCode.length >10){
					$scope.showError('企业编码长度不能大于10位');
					return;
				}
				var postData = {
					'name':$scope.enterpriseName,
					'code':$scope.enterpriseCode
				}
				$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/'+$scope.tenantId+'/enterprise',postData,'POST',function(data){
							console.log(data);
							if( data.code == 0){
				 				$scope.showSuccess('操作成功！');
				 				$("#addEnterprise").modal('toggle');
				 				//查询此租户下的所有企业
								$scope.getJson(resultPath+"/wisdomeatery/v1/tenants/"+$scope.tenantId+"/enterprise",'',"get",function(data){
										console.log(data)
										$scope.enterpriseDatas =data.data;
										$scope.enterpriseId = '';//置空企业ID
										$scope.regionDatas = [];
										$scope.regionId = '';//置空区域ID
										$scope.canteenDatas = [];
										
								})
				 			}else{
				 				$scope.showError(data.msg);
				 			}
					})
			}
			$scope.regionDatas = [];
			$scope.enterpriseId = '';
			//点击企业获取区域信息
			$scope.getRegion = function(id,name){
				$scope.enterpriseId = id;
				//查询此企业下的所有区域
				$scope.getJson(resultPath+"/wisdomeatery/v1/tenants/"+$scope.tenantId+"/enterprise/"+$scope.enterpriseId,'',"get",function(data){
						console.log(data)
						$scope.regionDatas = data.data;
						$scope.regionId = '';//置空区域ID
						$scope.canteenDatas = [];
				})
			}
			//展开区域新增模板
			$scope.addRegion = function(){
				if( $scope.enterpriseId == ''){
					$scope.showError('请选择企业');
					return;
				}
				$("#addRegion").modal('toggle');
			}
			//新增区域
			$scope.addRegion_true = function(){
				if( $scope.regionName.length > 45){
					$scope.showError('区域名称长度不能大于45位');
					return;
				}
				if( $scope.regionCode.length >10){
					$scope.showError('区域编码长度不能大于10位');
					return;
				}
				var postData = {
					'name':$scope.regionName,
					'code':$scope.regionCode
				}
				$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/'+$scope.tenantId+'/enterprise/'+$scope.enterpriseId+'/region',postData,'POST',function(data){
							console.log(data);
							if( data.code == 0){
				 				$scope.showSuccess('操作成功！');
				 				$("#addRegion").modal('toggle');
				 				//查询此企业下的所有区域
								$scope.getJson(resultPath+"/wisdomeatery/v1/tenants/"+$scope.tenantId+"/enterprise/"+$scope.enterpriseId,'',"get",function(data){
										console.log(data)
										$scope.regionDatas = data.data;
										$scope.regionId = '';//置空区域ID
										$scope.canteenDatas = [];
								})
				 			}else{
				 				$scope.showError(data.msg);
				 			}
					})
			}
			$scope.canteenDatas = [];
			$scope.regionId = '';
			//查询食堂
			$scope.getCanteen = function(id,name){
				$scope.regionId = id;
				//查询此区域下的所有食堂
				$scope.getJson(resultPath+"/wisdomeatery/v1/tenants/"+$scope.tenantId+"/enterprise/"+$scope.enterpriseId+"/region/"+$scope.regionId,'',"get",function(data){
						console.log(data)
						$scope.canteenDatas = data.data;
				})
			}
			//展开食堂新增模板
			$scope.addCanteen = function(){
				if( $scope.regionId == ''){
					$scope.showError('请选择区域');
					return;
				}
				$("#addCanteen").modal('toggle');
			}
			//食堂新增
			$scope.addCanteen_true = function(){
				if( $scope.canteenName.length > 45){
					$scope.showError('食堂名称长度不能大于45位');
					return;
				}
				var postData = {
					'name':$scope.canteenName
				}
				$scope.getJson(resultPath+'/wisdomeatery/v1/tenants/'+$scope.tenantId+'/enterprise/'+$scope.enterpriseId+'/region/'+$scope.regionId+'/canteen',postData,'POST',function(data){
							console.log(data);
							if( data.code == 0){
				 				$scope.showSuccess('操作成功！');
				 				$("#addCanteen").modal('toggle');
				 				//查询此区域下的所有食堂
								$scope.getJson(resultPath+"/wisdomeatery/v1/tenants/"+$scope.tenantId+"/enterprise/"+$scope.enterpriseId+"/region/"+$scope.regionId,'',"get",function(data){
										console.log(data)
										$scope.canteenDatas = data.data;
								})
				 			}else{
				 				$scope.showError(data.msg);
				 			}
					})
			}
			
			
			
			
						  
        });